import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeService from '@/views/Home/HomeService.js'
import Home from '../views/Home/Home'
import Login from '../views/Login/Login'
import Main from '../views/Main/Main.vue'
import Constant from '../views/Constant/Constant'
import Food from '../views/Food/Food'
import FoodCategory from "@/views/FoodCategory/FoodCategory"
import Notice from "@/views/Notice/Notice";
import Order from "@/views/Order/Order";
import User from "@/views/User/User";
import UserRights from "@/views/UserRights/UserRights";
import OrderDetail from '@/views/Order/OrderDetail/OrderDetail.vue'
import RedBag from '@/views/RedBag/RedBag.vue'
import RedBagDetail from '@/views/RedBag/RedBagDetail/RedBagDetail.vue'
import Point from '@/views/Point/Point.vue'
import PointDetail from '@/views/Point/PointDetail/PointDetail.vue'
import Vip from "@/views/Vip/Vip";
import ConstantCategory from "@/views/ConstantCategory/ConstantCategory";
import Restaurant from "@/views/Restaurant/Restaurant";
import Daily from '@/views/Finance/Daily/Daily.vue';
import Statistics from '@/views/Finance/Statistics/Statistics.vue';
import store from './../store'
import CustomerService from "@/views/CustomerService/CustomerService";

Vue.use(VueRouter)

const routes = [{
		path: '/',
		redirect: '/login'
	},
	{
		path: '/login',
		name: 'Login',
		component: Login
	},
	{
		path: '/home',
		name: 'Home',
		component: Home,
		children:[
			{
				path: '/home/main',
				name: 'Main',
				component: Main
			},
			{
				path: '/home/constant',
				name: 'Constant',
				component: Constant
			},
			{
				path: '/home/food',
				name: 'Food',
				component: Food
			},
			{
				path: '/home/foodCategory',
				name: 'FoodCategory',
				component: FoodCategory
			},
			{
				path: '/home/notice',
				name: 'Notice',
				component: Notice
			},
			{
				path: '/home/order',
				name: 'Order',
				component: Order
			},
			{
				path: '/home/orderDetail',
				name: 'OrderDetail',
				component: OrderDetail
			},
			{
				path: '/home/user',
				name: 'User',
				component: User
			},
			{
				path: '/home/userRights',
				name: 'UserRights',
				component: UserRights
			},
			{
				path: '/home/redBag',
				name: 'RedBag',
				component: RedBag
			},
			{
				path: '/home/redBagDetail',
				name: 'RedBagDetail',
				component: RedBagDetail
			},
			{
				path: '/home/point',
				name: 'Point',
				component: Point
			},
			{
				path: '/home/pointDetail',
				name: 'PointDetail',
				component: PointDetail
			},
			{
				path: '/home/vip',
				name: 'Vip',
				component: Vip
			},
			{
				path: '/home/constantCategory',
				name: 'ConstantCategory',
				component: ConstantCategory
			},
			{
				path: '/home/restaurant',
				name: 'Restaurant',
				component: Restaurant
			},
			{
				path: '/home/daily',
				name: 'Daily',
				component: Daily
			},
			{
				path: '/home/statistics',
				name: 'Statistics',
				component: Statistics
			},
			{
				path: '/home/customerService',
				name: 'CustomerService',
				component: CustomerService
			},
		],
		redirect: '/home/main'
	},
]

// 解决ElementUI导航栏中的vue-router在3.0版本以上重复点菜单报错问题
const originalPush = VueRouter.prototype.push
   VueRouter.prototype.push = function push(location) {
   return originalPush.call(this, location).catch(err => err)
}

const router = new VueRouter({
	mode: 'history',
	base: process.env.BASE_URL,
	routes
})

router.beforeEach((to,from,next)=>{
	if(to.name != 'Login'){
		//每次都发请求，感觉不太好
		// HomeService.getlogin(res=>{
		// 	if(!res.loginUser){
		// 		next({
		// 			name: 'Login',
		// 		})
		// 	}else{
		// 		next();
		// 	}
		// })
		if(!store.getters.getisLogin){
			next({
				name: 'Login',
			})
		}else{
			next();
		}
	}else{
		next();
	}

});

export default router
